package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntOrderInfoMapper;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户订单表操作接口.
 */
public class BspsMchntOrderInfoMapperImpl implements BspsMchntOrderInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<MchntOrderInfo> selectBspsMchntOrderInfoByMchntNo(String instId, String mchntNo, String beginDate, String endDate, String transStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntOrderInfoByMchntNo");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setMchntNo(mchntNo);
        mchntOrderInfo.setTxnDate(beginDate);
        mchntOrderInfo.setRemark1(endDate);
        mchntOrderInfo.setTransStatus(transStatus);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号,用户Id查询商户订单信息失败,instId:<{}>,mchntNo:<{}>,beginDate:<{}>,endDate:<{}>", instId, mchntNo, beginDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectBspsMchntOrderInfoByMchntNoAndUserId(String instId, String mchntNo, String beginDate, String endDate, String userId, String transStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntOrderInfoByMchntNoAndUserId");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setMchntNo(mchntNo);
        mchntOrderInfo.setTxnDate(beginDate);
        mchntOrderInfo.setRemark1(endDate);
        mchntOrderInfo.setUserId(userId);
        mchntOrderInfo.setTransStatus(transStatus);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号,用户Id查询商户订单信息失败,instId:<{}>,mchntNo:<{}>,beginDate:<{}>,endDate:<{}>,userId：<{}>", instId, mchntNo, beginDate, endDate, userId);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }
}
